<?php

/**
 * Created by PhpStorm.
 * User: ls721
 * Date: 2016/10/10 0010
 * Time: 17:17
 */
class Domain_Event
{

    public function createEvent($name, $illness, $patient_id, $call_phone, $provice, $city, $district, $detail)
    {
        $rs = array();
        $model = new Model_Event;
        $Eid = uniqid('E');
        $Lid = uniqid("L");

        $modelArea = new Model_Area();
        $pro = $modelArea->get($provice . "0000", 'name')['name'];
        $cit = $modelArea->get($city . "00", 'name')['name'];
        $dis = $modelArea->get($district, 'name')['name'];
        $address = $pro . $cit . $dis . $detail;
        $rs = $this->getLocationFromApi($address);
        $model1 = new Model_Location();
        $lng = "";
        $lat = "";
        if ($rs['Status'] == 0) {
            $result = $rs["result"];
            $Location = $result['location'];
            $lng = $Location['lng'];
            $lat = $Location['lat'];
        }
        $title = $address . "一市民" . $illness;
        $model->insert(array('id' => $Eid, 'Name' => $name, 'Illness' => $illness, 'Title' => $title,
            'Patient_id' => $patient_id, 'CallPhone' => $call_phone, 'Location' => $dis . $detail, 'Location_id' => $Lid));
        $model1->insert(array(
            'id' => $Lid,
            'provice' => $pro,
            'city' => $cit,
            'district' => $dis,
            'detail' => $detail,
            'Address' => $address,
            'Lng' => $lng,
            'Lat' => $lat
        ));
        return $Eid;
    }

    public function listEvents()
    {
        $model = new Model_Event();


        return $model->listEvents();

    }

    public function setDoctorToEvent($event_id, $doctor_id)
    {
        $model = new Model_Dispatch();


        if ($model->isExist($event_id, $doctor_id) == 0) {
            $model->insert(array('Event_id' => $event_id, 'Doctor_id' => $doctor_id));
            return "success";
        } else {
            return null;
        }
    }

    public function getEventsOfDoctor($doctor_id)
    {
        $model = new Model_Event();
        $rs = $model->getEventsOfDoctor($doctor_id);
        return $rs;

    }

    public function setComment($comment_id, $event_id, $doctor_id, $content)
    {
        $model = new Model_Comment();
        $Cid = uniqid("C");
        if (empty($comment_id)) {
            $model->insert(array(
                'id' => $Cid,
                'Event_id' => $event_id,
                'Doctor_id' => $doctor_id,
                'Content' => $content,
            ));
        } else {
            $model->update($comment_id, array(

                'Event_id' => $event_id,
                'Doctor_id' => $doctor_id,
                'Content' => $content,
            ));
        }

        return $model->get($Cid);
    }

    public function setCommentImage($comment_id, $image)
    {
        $model = new Model_Image();
        DI()->ucloud->set('default_path', 'comment');
        DI()->ucloud->set('save_path', date('Y/m/d'));

        //新增修改文件名设置上传的文件名称
        $Iid = uniqid("I");

        DI()->ucloud->set('file_name', $Iid);

        //上传表单名
        $rs = DI()->ucloud->upfile($image);
        $model->insert(array('Comment_Id' => $comment_id, 'Image_id' => $Iid, 'image' => $rs['url']));
        return $model->getImagesOfComment($comment_id);


    }

    public function getLocationFromApi($address)
    {

        $url = "http://api.map.baidu.com/geocoder/v2/?address=" . $address . "&output=json&ak=sOaMjMU3xLTr5cq7hOC5rPufKy7xLHl8";
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, 3000);

        $rs = curl_exec($ch);

        curl_close($ch);
        $info = json_decode($rs, true);
        return $info;

    }

    public function getEventInfo($event_id)
    {
        $model = new Model_Event();
        return $model->getEventInfo($event_id);

    }




}